package com.encryption.feature.doc.ui.component
{
	
	import com.encryption.util.FileUtils;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import mx.controls.Alert;
	import mx.controls.ProgressBar;
	import mx.core.FlexGlobals;
	import mx.core.UIComponent;
	import mx.events.CloseEvent;
	import mx.managers.PopUpManager;
	
	public class FileDownload
	{
		
		/**
		 * 显示的文件名称截取长度
		 */
		private var fileNameLength:int;
		
		/**
		 * 单一文件
		 */
		private var file:FileReference;
		
		/**
		 * 下载的全路径
		 */
		private var url:String;
		
		/**
		 * 文件类型
		 */
		private var fileType:String;
		
		/**
		 * 文件默认名称
		 */
		private var defaultName:String;
		
		private var _progressBar:ProgressBar;
		
		public function FileDownload()
		{
			init();
		}
		
		/**
		 * 初始化事件
		 */ 						
		private function init():void
		{
			_progressBar = new ProgressBar();
			
			file = new FileReference();
			file.addEventListener(Event.OPEN, openHandler);
			file.addEventListener(Event.SELECT, selectFileHandle);
			file.addEventListener(Event.CANCEL,cancelHandle);
			file.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandle);
			file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
			file.addEventListener(Event.COMPLETE, completeHandler);
			
			_progressBar.visible = false;
		}
		
		/**
		 * 设置主要信息
		 */
		public function load(url:String,defaultName:String,fileNameLength:int = 0):void
		{
			this.url = url;
			this.fileType = this.url.substring(this.url.lastIndexOf("."),this.url.length);
			this.defaultName = defaultName  + this.fileType ;
			this.fileNameLength = fileNameLength;				
		}			
		
		/**
		 * 点击下载事件
		 */			
		public function download():void
		{
			var request:URLRequest = new URLRequest();
			request.method = URLRequestMethod.POST;
			request.url = encodeURI(this.url);
			
			file.download(request,this.defaultName);
		}
		
		/**
		 * 取消下载
		 */
		private function cancel_click():void
		{
			Alert.okLabel = "是";
			Alert.noLabel = "否";
			var alert:Alert = Alert.show("取消下载文件？","提示",Alert.OK|Alert.NO,null,alertHandle);	
		}
		
		private function alertHandle(evt:CloseEvent):void
		{
			if(evt.detail == Alert.OK)
			{
				file.cancel();
				
				_progressBar.visible = false;
				_progressBar.includeInLayout = false;
			}
		}
		
		/**
		 * 打开文件浏览
		 * 
		 */ 
		private function openHandler(evt:Event):void
		{
		}
		
		private function selectFileHandle(event:Event):void
		{
			download();
			_progressBar.visible = true;
			_progressBar.includeInLayout = true;
		}
		
		/**
		 * 文件下载异常事件
		 */
		private function ioErrorHandle(evt:IOErrorEvent):void
		{
			Alert.show("网路错误，请稍后下载!","错误");
			_progressBar.visible = false;
			_progressBar.includeInLayout = false;
		}
		
		/**
		 * 文件下载进度事件
		 */
		private function progressHandler(evt:ProgressEvent):void
		{
			var currentName:String = FileReference(evt.currentTarget).name;
			if(fileNameLength != 0)	currentName = (currentName.length < fileNameLength)?currentName:currentName.substr(0,fileNameLength);
			var percent:int = int(evt.bytesLoaded / evt.bytesTotal * 100);
			
			_progressBar.setProgress(percent,100);
			_progressBar.label += "下载 " + currentName + " 进度: " + percent + "% " ;
			_progressBar.label += "当前已下载" + FileUtils.formatFileSize(evt.bytesLoaded) + 
				"，共" + FileUtils.formatFileSize(evt.bytesTotal);
			
			PopUpManager.addPopUp(_progressBar, FlexGlobals.topLevelApplication as UIComponent);
			PopUpManager.centerPopUp(_progressBar);
		}
		
		/**
		 * 文件下载事件
		 */
		private function completeHandler(evt:Event):void
		{
			Alert.show("文件下载成功","成功");
			_progressBar.visible = false;
			_progressBar.includeInLayout = false;
		}
		
		/**
		 * 取消选择事件
		 */
		private function cancelHandle(evt:Event):void
		{
			_progressBar.visible = false;
			_progressBar.includeInLayout = false;
		}
	}
}